import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:riverpod_t/base/base_async_consumer.dart';
import 'package:riverpod_t/page/car/data/model/info_model.dart';
import 'package:riverpod_t/page/car/event/car_info_op_event.dart';
import 'package:riverpod_t/page/car/river/car_info_op_river.dart';
import 'package:riverpod_t/page/car/state/car_op_state.dart';
import 'package:riverpod_t/page/car/widget/remove_bottom_dialog.dart';
import 'package:riverpod_t/until/app_util.dart';
import 'package:riverpod_t/until/dialog_until.dart';
import 'package:riverpod_t/widget/app_net_image.dart';

class CarInfoWidget extends StatelessWidget {
  final InfoModel model;
  final VoidCallback onTap;
  final VoidCallback? onRemove;

  const CarInfoWidget({super.key, required this.model, required this.onTap, this.onRemove});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(onTap: onTap, child: _buildContent());
  }

  Widget _buildContent() {
    return Container(
      width: double.infinity,
      color: Colors.white,
      padding: EdgeInsets.only(left: 10.w, right: 10.w, top: 14.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildImageSection(),
          _buildTitle(),
          SizedBox(height: 10.h),
          _buildBottomRow(),
          SizedBox(height: 14.h),
          _buildDivider(),
        ],
      ),
    );
  }

  Widget _buildImageSection() {
    if (AppUtil.isEmpty(model.newsPics)) return const SizedBox();
    final hasMultipleImages = model.newsPics.length >= 3;
    final imageCount = hasMultipleImages ? 3 : 1;
    final imageWidth = (1.sw - (hasMultipleImages ? 40.w : 20.w)) / imageCount;
    final imageHeight = imageWidth / 3 * 2;

    if (hasMultipleImages) {
      return Row(
        children:
            model.newsPics.asMap().keys.map((index) {
              return Padding(
                padding: EdgeInsets.only(right: index < 2 ? 10.w : 0),
                child: AppNetImage(
                  imageUrl: model.newsPics[index],
                  width: imageWidth,
                  height: imageHeight,
                  radius: 5.h,
                ),
              );
            }).toList(),
      );
    }

    return AppNetImage(
      imageUrl: model.videoCover,
      width: imageWidth,
      height: imageHeight,
      radius: 5.h,
    );
  }

  Widget _buildTitle() {
    return Text(
      model.title,
      style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w500),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  Widget _buildBottomRow() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [_buildAuthorInfo(), _buildActionButtons()],
    );
  }

  Widget _buildAuthorInfo() {
    return Row(
      children: [
        AppNetImage(imageUrl: model.mcnIcon, width: 20.w, height: 20.w, radius: 10.w),
        SizedBox(width: 5.w),
        Text(model.mcnRealName, style: TextStyle(fontSize: 12.sp)),
        SizedBox(width: 5.w),
        Text(model.createTimeStr, style: TextStyle(fontSize: 12.sp, color: Colors.grey)),
      ],
    );
  }

  /// 点赞widget
  Widget _zanWidget() {
    return BaseAsyncConsumer<CarInfoOpState>(
      provider: carInfoOpRiverProvider,
      builder: (BuildContext context, CarInfoOpState state, WidgetRef ref) {
        return GestureDetector(
          onTap: () {
            ref.read(carInfoOpRiverProvider.notifier).addLike(CarInfoLikeOp(model: model));
          },
          child: _buildStatItem(
            icon: "assets/images/zan_nonal.png",
            value: '点赞',
            isLiked: model.isLike == true,
          ),
        );
      },
    );
  }

  Widget _buildActionButtons() {
    return Row(
      children: [
        _zanWidget(),
        SizedBox(width: 10.w),
        _removeItem(icon: "assets/images/ic_graydelete.png", value: '删除'),
      ],
    );
  }

  Widget _removeItem({required String icon, required String value}) {
    return InkWell(
      onTap: () {
        showBottomDialog(
          context: AppUtil.getContext()!,
          builder: CommonBottomDialog(
            title: '提示',
            content:
                '确认删除吗? 删除了可就回不来了啊, 哈哈哈哈, 逗你呢, 你删除吧, 只是在内存中删除的, 下拉刷新后就又回来了确认删除吗? 删除了可就回不来了啊, 哈哈哈哈, 逗你呢, 你删除吧, 只是在内存中删除的, 下拉刷新后就又回来了',
            model: model,
          ),
        );
      },
      child: Row(
        children: [
          Image.asset(icon, width: 16.w, height: 16.w),
          SizedBox(width: 5.w),
          Text(value, style: TextStyle(fontSize: 12.sp, color: Colors.grey)),
        ],
      ),
    );
  }

  Widget _buildStatItem({required String icon, required String value, bool isLiked = false}) {
    return Row(
      children: [
        Image.asset(
          icon,
          width: isLiked ? 16.w : 16.w,
          height: isLiked ? 16.w : 16.w,
          color: isLiked ? Colors.red : Colors.grey,
        ),
        SizedBox(width: 5.w),
        Text(value, style: TextStyle(fontSize: 12.sp, color: Colors.grey)),
      ],
    );
  }

  Widget _buildDivider() {
    return Container(
      height: 1.h,
      width: double.infinity,
      margin: EdgeInsets.only(right: 10.w),
      color: Colors.black12,
    );
  }
}
